{extend name="public/base"}

{block name="x-body"}
  <div class="x-body">
    <div class="layui-row">
      <form class="layui-form layui-col-md12 x-so" method="get">
        <div class="layui-input-inline">
          <select name="position_id">
            <option value="">全部广告</option>
            {volist name="ad_position" id="v"}
            <option value="{$v.id}" {$search_key.position_id == $v.id ? 'selected' : ''}>{$v.position_name}</option>
            {/volist}
          </select>
        </div>
        <input type="text" name="ad_name"  placeholder="请输入广告名" autocomplete="off" class="layui-input" value="{$search_key.ad_name}">
        <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
      </form>
    </div>
    <xblock>
      <button class="layui-btn layui-btn-danger" onclick="delAll('{:url(\'ad/ad_del\')}')"><i class="layui-icon"></i>批量删除</button>
      <button class="layui-btn" onclick="x_admin_show('{$navigate[1]}','{:url(\'ad/ad_form\')}')"><i class="layui-icon"></i>添加</button>
      <span class="x-right" style="line-height:40px">共有数据：{$list -> total()} 条</span>
    </xblock>
    <table class="layui-table layui-form">
      <thead>
        <tr>
          <th>
            <div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
          </th>
          <th>ID</th>
          <th>广告名称</th>
          <th>广告图片</th>
          <th>广告位置</th>
          <th width="50">排序</th>
          <th width="50">显示</th>
          <th width="50">新窗口</th>
          <th>操作</th>
      </thead>
      <tbody>
        {volist name="list" id="v"}
          <tr>
            <td>
              <div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id='{$v.id}'><i class="layui-icon">&#xe605;</i></div>
            </td>
            <td>{$v.id}</td>
            <td>{$v.ad_name}</td>
            <td><img src="{:config('imgRoute')}{$v.ad_img}" alt="" style="max-height: 50px;"></td>
            <td>{$v.position_name}</td>
            <td><input type="text" class="layui-input x-sort" name="sort" data-id="{$v.id}" value="{$v.sort}" onfocus="isNum(this)"></td>
            <td>
              <input type="checkbox" name="is_show" data-id="{$v.id}" data-status="{$v.is_show}" lay-text="是|否" {if $v.is_show == 1}checked=""{/if} lay-skin="switch">
            </td>
            <td>
              <input type="checkbox" name="target" data-id="{$v.id}" data-status="{$v.target}" lay-text="是|否" {if $v.target == 1}checked=""{/if} lay-skin="switch">
            </td>
            <td class="td-manage">
              <a title="编辑"  onclick="x_admin_show('{$navigate[1]}', '{:url(\'ad/ad_form\', [\'id\' => $v.id])}')" href="javascript:;">
                <i class="layui-icon">&#xe642;</i>
              </a>
              <a title="删除" onclick="member_del(this,'{$v.id}', '{:url(\'ad/ad_del\')}')" href="javascript:;">
                <i class="layui-icon">&#xe640;</i>
              </a>
            </td>
          </tr>
        {/volist}
      </tbody>
    </table>
    <div class="page">
      <div>
        {$list -> render()|raw}
      </div>
    </div>

  </div>

<script>
    $(document).ready(function (){
        /**
         * 更新排序
         */
        sort('{:url("ad/adSort")}');

        /**
         * 更新状态
         */
        $('table').on('click', '.layui-form-switch', function (){
            var id = $(this).prev().data('id');
            var status_name = $(this).prev().attr('name');
            var that = $(this).prev();
            var status = that.data('status') == 0 ? 1 : 0;
            var data = {'id': id};
            data[status_name] = status;
            $.post('{:url("ad/adStatus")}', data, function(res){
                if(res.code == 1){
                    that.data('status', status);
                }
                layer.msg(res.msg, {icon:1,time:1000});
            }, 'json');
        })
    });
</script>
{/block}